package com.site.service.impl;

import com.night.common.entity.logistical.Appointment;
import com.night.common.entity.logistical.vo.AppointmentVO;
import com.site.mapper.AppointmentMapper;
import com.site.service.AppointmentService;
import com.site.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: AppointmentMapperImpl
 * @author: LJC
 * @date: 2022/10/20-17:26
 */
@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    //查询所有预约信息
    @Override
    public List<Appointment> getAllAppointment() {
        return appointmentMapper.getAll();
    }

    //新增预约信息
    @Override
    public void addAppointment(AppointmentVO appointmentVO) throws ParseException {
        //
        Appointment appointment = new Appointment();
        Integer userId = appointmentVO.getUserId();
        Integer siteId = appointmentVO.getSiteId();
        String userName = appointmentVO.getUserName();
        String appointmentDate = appointmentVO.getAppointmentDate(); //2022-10-28
        String stime = appointmentVO.getStime(); //13:00
        String etime = appointmentVO.getEtime(); //17:00
        String userPhone = appointmentVO.getUserPhone();
        //根据用户id查找用户信息
//        Teacher student = studentMapper.getByStudent();

        //时间转换
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = d.parse(appointmentDate);
        //开始时间
        String st = appointmentDate + " " + stime;
        Date sdt = dt.parse(st);
        //结束时间
        String et = appointmentDate + " " + etime;
        Date edt = dt.parse(et);

        appointment.setUserId(userId);
        appointment.setSiteId(siteId);
        appointment.setAppointmentDate(date);
        appointment.setStartTime(sdt);
        appointment.setEndTime(edt);
        appointment.setUserPhone(userPhone);
        appointment.setUserName(userName);
        appointmentMapper.addAppointment(appointment);
    }

    //修改预约信息
    @Override
    public void updateAppointment(Appointment appointment) {
        appointmentMapper.updateAppointment(appointment);
    }

    //根据id查询预约信息
    @Override
    public Appointment getAppointmentById(Appointment appointment) {
        return appointmentMapper.getAppointmentById(appointment);
    }

    //根据用户id查询
    @Override
    public List<Appointment> getAppointmentByUserId(Appointment appointment) {
        return appointmentMapper.getAppointmentByUserId(appointment);
    }

    //判断开始时间和结束时间
    @Override
    public String getSTandET(Appointment appointment) {
        //获取时间
        Date startTime = appointment.getStartTime();
        Date endTime = appointment.getEndTime();
        //判断开始时间,不能比所有结束时间少
        List<Appointment> list = appointmentMapper.getAll();
        for (Appointment amt : list) {

        }

        return null;
    }
}
